package com.hxc.shop.service;

import com.hxc.shop.bean.ShopGoodsCategory;
import com.hxc.shop.common.ResultCode;
import com.hxc.shop.common.ResultVO;
import com.hxc.shop.mapper.Mysql.GoodsCategoryMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GoodsCategoryService {
    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;

    public ResultVO getCategoryByCount(Integer count){
        List<ShopGoodsCategory> goodsCategoryList = new ArrayList<>();

        try {
            goodsCategoryList = goodsCategoryMapper.getCategoryByCount(count);
        }catch (Exception e) {
            log.error("获取商品分类数据失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"获取商品分类数据失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,goodsCategoryList);
    }

    public ResultVO getAllCategory() {
        List<ShopGoodsCategory> goodsCategoryList = new ArrayList<>();

        try {
            goodsCategoryList = goodsCategoryMapper.getAllCategory();
        }catch (Exception e) {
            log.error("获取商品分类数据失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"获取商品分类数据失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,goodsCategoryList);
    }

    public ResultVO getCategoryByName(String categoryName) {
        List<ShopGoodsCategory> goodsCategoryList = new ArrayList<>();
        try {
            goodsCategoryList = goodsCategoryMapper.getCategoryByName("%"+categoryName+"%");
        }catch (Exception e) {
            log.error("获取商品分类数据失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"获取商品分类数据失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,goodsCategoryList);
    }

    public ResultVO updateCategory(Integer id,String goodsCategory) {
        if (id == 0 || id == null) {
            return new ResultVO(ResultCode.ERROR.getCode(),"未获取到商品分类编号，无法修改!");
        }
        if (StringUtils.isEmpty(goodsCategory)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品分类不能为空!");
        }
        Integer checkId = 0;
        try {
            checkId = goodsCategoryMapper.getCategoryIdByCategoryId(id);
        }catch (Exception e) {
            log.error("查找商品分类编号失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找商品分类编号失败:"+e.getMessage());
        }
        if (checkId == null || checkId == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品分类编号:"+id);
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ShopGoodsCategory shopGoodsCategory = new ShopGoodsCategory();
        shopGoodsCategory.setId(id);
        shopGoodsCategory.setGoodsCategory(goodsCategory);
        shopGoodsCategory.setUpdateTime(sdf.format(new Date()));
        try {
            goodsCategoryMapper.updateGoodsCategory(shopGoodsCategory);
        }catch (Exception e) {
            log.error("修改商品分类失败!"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"修改商品分类失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS.getCode(),"修改商品分类成功:"+goodsCategory);
    }

    public ResultVO insertCategory(String goodsCategory) {
        if (StringUtils.isEmpty(goodsCategory)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品分类不能为空!");
        }

        // 判断是否有相同的商品分类名称
        String checkName = "";
        try {
            checkName = goodsCategoryMapper.findSameGoodsCategoryName(goodsCategory);
        }catch (Exception e) {
            log.error("查找相同的商品分类名称失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"比对相同商品分类名称失败:"+e.getMessage());
        }
        if (!StringUtils.isEmpty(checkName)) {
            if (checkName.equals(goodsCategory)) {
                return new ResultVO(ResultCode.ERROR.getCode(),"已有相同的商品分类名称!");
            }
        }

        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ShopGoodsCategory shopGoodsCategory = new ShopGoodsCategory();
        shopGoodsCategory.setGoodsCategory(goodsCategory);
        shopGoodsCategory.setCreateTime(sdf.format(new Date()));
        shopGoodsCategory.setUpdateTime(sdf.format(new Date()));
        try {
            goodsCategoryMapper.insertGoodsCategory(shopGoodsCategory);
        }catch (Exception e) {
            log.error("插入商品分类失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"插入商品分类失败:"+e.getMessage());
        }

        return new ResultVO(ResultCode.SUCCESS.getCode(),"插入商品分类成功:"+goodsCategory);
    }

    public ResultVO deleteCategory(Integer id) {
        if (id ==0 || id == null) {
            return new ResultVO(ResultCode.ERROR.getCode(),"未获取到商品分类编号，删除失败!");
        }
        Integer checkId = 0;
        try {
            checkId = goodsCategoryMapper.getCategoryIdByCategoryId(id);
        }catch (Exception e) {
            log.error("查找商品分类编号失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找商品分类编号失败:"+e.getMessage());
        }
        if (checkId == null || checkId == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品分类编号:"+id);
        }
        try {
            goodsCategoryMapper.deleteGoodsCategoryById(id);
        }catch (Exception e) {
            log.error("删除商品分类失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"删除商品分类失败:"+e.getMessage());
        }

        return new ResultVO(ResultCode.SUCCESS.getCode(),"删除商品分类成功!商品分类编号:"+id);
    }
}
